package com.kingcheer.dao.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;

public class ShardingJdbcConfig {

	@Autowired
	private DataSource primaryDataSource;

	@Autowired
	@Qualifier("secondaryDataSource")
	private DataSource secondaryDataSource;

	private DataSource shardingDataSource;

	@PostConstruct
	public void init() {
		Map<String, DataSource> map = new HashMap<String, DataSource>();
		// 配置数据名称 与配置文件中的datasource name 一致
		map.put("testdb0", primaryDataSource);
		map.put("testdb1", secondaryDataSource);
		DataSourceRule dataSourceRule = new DataSourceRule(map);
		List<TableRule> tableRuleList = new ArrayList<TableRule>();
		List<String> pList = new ArrayList<String>();
		for (int i = 0; i < 2; i++) {
			pList.add("t_order_" + i);
		}
		// 表分片配置
		tableRuleList.add(new TableRule.TableRuleBuilder("t_order").actualTables(pList).dataSourceRule(dataSourceRule)
				.tableShardingStrategy(new TableShardingStrategy("order_id", new TableShardingAlgorithm())).build());
		ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule)
				.databaseShardingStrategy(
						// 数据库分片配置
						new DatabaseShardingStrategy("user_id", new SingleKeyModuloDatabaseShardingAlgorithm()))
				.tableRules(tableRuleList).build();
		shardingDataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
	}

	public DataSource getShardingDataSource() {
		return shardingDataSource;
	}
}
